﻿package cn.hadron.aa.action.admin;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.hadron.aa.entity.Article;
import cn.hadron.aa.entity.Channel;
import cn.hadron.aa.service.*;

import com.opensymphony.xwork2.ActionSupport;
@Controller("articleAdmin")//避免重名
@Scope("prototype") 
@ParentPackage("admin")
@Namespace("/admin")
public class ArticleAction extends ActionSupport {
	private int id;
	private String content;
	private Article article;
	private List<Channel> clist;
	private String message;
	@Resource(name="articleService")
	private ArticleService as;
	@Resource(name="channelService")
	private ChannelService cs;
	//查看文章
	@Action(value="detail", results={@Result(name="success",location="/admin/ok.jsp")})
	public String detail() throws Exception {
		article=as.get(id);
		return SUCCESS;
	}
	//添加新文章
	@Action(value="add",
			interceptorRefs={ 
        	   @InterceptorRef("defaultStack"),  
               @InterceptorRef("authority")  
            },
			results={
			   @Result(name="success",location="/admin/ok.jsp"),
			   @Result(name="input",location="/admin/add.jsp")})
	public String add() throws Exception {
		if(article==null){//准备Channel数据
			clist=cs.getAllChannel();
			return INPUT;
		}
		article.setAddtime(new Date());
		article.setContent(content);
		as.add(article);
		Channel c=cs.get(article.getChannel().getId());
		article.getChannel().setName(c.getName());
		message = "发布成功！";
		return SUCCESS;
	}
	//删除文章
	@Action(value="delete", 
			interceptorRefs={ 
     	       @InterceptorRef("defaultStack"),  
               @InterceptorRef("authority")  
            },
			results={@Result(name="success",type="redirect",location="/admin/list")})
	public String delete()throws Exception {
		as.delete(id);
		return SUCCESS;
	}
	//修改文章
	@Action(value="edit", 
			interceptorRefs={ 
     	       @InterceptorRef("defaultStack"),  
               @InterceptorRef("authority")  
            },
			results={
			   @Result(name="success",location="/admin/ok.jsp"),
			   @Result(name="input",location="/admin/edit.jsp")})
	public String edit() throws Exception {
		if(article==null){//准备编辑数据
			article=as.get(id);
			clist=cs.getAllChannel();
			return INPUT;
		}
		article.setAddtime(new Date());
		article.setContent(content);
		as.modify(article);
		Channel c=cs.get(article.getChannel().getId());
		article.getChannel().setName(c.getName());
		message="更新成功！";
		return SUCCESS;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public Article getArticle() {
		return article;
	}
	public void setArticle(Article article) {
		this.article = article;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public ArticleService getAs() {
		return as;
	}
	public void setAs(ArticleService as) {
		this.as = as;
	}
	public List<Channel> getClist() {
		return clist;
	}
	public void setClist(List<Channel> clist) {
		this.clist = clist;
	}
}
